package com.hyy.dao.daoImpl;

import com.hyy.bean.Cart;
import com.hyy.dao.CartDao;
import com.hyy.util.JdbcUtil;
import com.mysql.cj.util.EscapeTokenizer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
	@Override
	public boolean insert(Cart cart) throws SQLException {
		if (isExist(cart.getUid(),cart.getMid())){
			addNum(cart.getUid(),cart.getMid(),cart.getNum());
		}
		else{
			Connection connection = JdbcUtil.getConnection();
			String sql = "insert into Cart(uid, mid, num) values (?,?,?)";
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setLong(1,cart.getUid());
			pstmt.setLong(2,cart.getMid());
			pstmt.setLong(3,cart.getNum());
			int flag = pstmt.executeUpdate();
			JdbcUtil.release(connection,pstmt);
			return flag==1?true:false;
		}
		return false;
	}


	@Override
	public boolean deleteMyCart(Long uid,Long mid) throws SQLException {
		Connection connection = JdbcUtil.getConnection();
		String sql = "delete from cart where uid=? and mid=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setLong(1,uid);
		pstmt.setLong(2,mid);
		int flag = pstmt.executeUpdate();
		JdbcUtil.release(connection,pstmt);
		return flag==1?true:false;
	}

	@Override
	public boolean updateNum(Cart cart) throws SQLException {
		Connection connection = JdbcUtil.getConnection();
		String sql = "update cart set num = ? where mid=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setLong(1,cart.getNum());
		pstmt.setLong(2,cart.getMid());
		int flag = pstmt.executeUpdate();
		JdbcUtil.release(connection,pstmt);
		return flag==1?true:false;
	}

	@Override
	public boolean isExist(long uid,long mid) throws SQLException {
		Connection connection = JdbcUtil.getConnection();
		String sql = "select cid from cart where uid=? and mid=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setLong(1,uid);
		pstmt.setLong(2,mid);
		ResultSet rs = pstmt.executeQuery();
		int flag = 0;
		while (rs.next()){
			flag=1;
		}
		JdbcUtil.release(connection,pstmt);
		return flag==1?true:false;
	}

	@Override
	public boolean addNum(long uid,long mid,long num) throws SQLException {
		Connection connection = JdbcUtil.getConnection();
		String sql = "update cart set num = num + ? where uid=? and mid=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setLong(1,num);
		pstmt.setLong(2,uid);
		pstmt.setLong(3,mid);
		int flag = pstmt.executeUpdate();
		JdbcUtil.release(connection,pstmt);
		return flag==1?true:false;
	}

	@Override
	public List<Cart> showAllByUid(long uid) throws SQLException {
		List<Cart> carts = new ArrayList<Cart>();
		Connection connection = JdbcUtil.getConnection();
		String sql = "select cid,uid,mid,num from cart where uid = ?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setLong(1,uid);
		ResultSet rs = pstmt.executeQuery();
		while (rs.next()){
			carts.add(new Cart(rs.getLong(1),rs.getLong(2),rs.getLong(3), rs.getLong(4)));
		}
		return carts;
	}
}
